app.controller("z-maternityLeave-c", [ '$scope','$rootScope','$z-http','$sce','z-maternityLeave-s','z-dictionary-s','z-corporation-s','z-department-s','z-punchTime-s','z-attendance-s','z-edit-menu-s','z-employee-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,PunchTimeService,AttendanceService,MenuService,EmployeeService,$filter) {
		$scope.viewModel = {};
		$scope.editModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.effectiveDate = new Date();
		//编辑使用
		$scope.editModel = {};
		$scope.editModel.maternityLeave={};
		$scope.editModel.title="";
		$scope.editModel.employeeLock = false;
		//查询页面名称
		MenuService.getMenuByUrl({url:'maternityLeave'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		//加载地区数据
		$scope.viewModel.bindSelectPlace = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"7"
			},
			function(data){
				$scope.viewModel.PlaceData = data;
				$scope.editModel.PlaceData = data;
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectPlace();
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getRequestAuthorityList({
				pageCount : $scope.viewModel.perPage,
				pageNo : $scope.viewModel.rowCount,
				regionType:$scope.viewModel.regionType,
				employeeList:$scope.viewModel.employeeList,
			},
			function(data){
				$scope.viewModel.dataList = data.requestAuthorityBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			 //小时控件jquery-timepicker
		    $('#wStsp').hunterTimePicker('wStartTime',{
		        callback: function(time){
					$scope.editModel.special.startTime=time;
		        }
		    });
		    $('#wEdsp').hunterTimePicker('wEndTime',{
		        callback: function(time){
					$scope.editModel.special.endTime=time;
		        }
		    });
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.employeeLock = false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.editModel.type = true;
			$scope.editModel.title="新增产假分配";
			$scope.editModel.maternityLeave={};
			$scope.editModel.maternityLeave.requestType=13;
			$scope.viewModel.load();
			//验证不能为空
			$scope.editForm.employeeName.$dirty=false;
			$scope.editForm.holidayQuota.$dirty=false;
		};
		//修正
		$scope.viewModel.update=function(maternityLeave){
			$scope.editModel.type = false;
			$scope.editModel.title="修改产假分配";
			$scope.editModel.maternityLeave=angular.copy(maternityLeave);
			$scope.editModel.maternityLeave.requestType=13;
			$scope.editModel.employeeLock = true;
			$scope.viewModel.load();
		};
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.employeeName.$dirty){
            		$scope.editForm.employeeName.$dirty=true;
            		$scope.editForm.employeeName.$invalid=true;
            	}
            	if(!$scope.editForm.holidayQuota.$dirty){
            		$scope.editForm.holidayQuota.$dirty=true;
            	}
                return true;
            }else{
            	if($scope.editModel.maternityLeave.employeeId==null){
            		$rootScope.alertError("员工信息未选择，请选择。");
            		return true;
            	}else{
            		return false;
            	}
            }
        };
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.editModel.employeeLock = false;
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveRequestAuthority($scope.editModel.maternityLeave,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}else{
				Service.updateRequestAuthority($scope.editModel.maternityLeave,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}
		};
		// 删除
		$scope.viewModel.del = function(maternityLeave){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteRequestAuthority({
					requestAuthorityId:maternityLeave.requestAuthorityId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		}
		/*   人员选择   */
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.perPage = "10";
		$scope.viewEmployeeModel.rowCount = "1";
		// 人员选择按钮
		$scope.editModel.chooseUpde = function(){
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.title="人员选择";
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.employeeLevelList=[];
			$scope.viewEmployeeModel.level=null;
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#mlsCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#mlslevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#mlsCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#mlsCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#mlsDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#mlsDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#mlsDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			EmployeeService.getEmployeeList({
				rowCount : $scope.viewEmployeeModel.perPage,
				perPage : $scope.viewEmployeeModel.rowCount,
				sex:"2",
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.isLoading=false;
			},function(error) {
				$scope.viewEmployeeModel.isLoading=false;
			});
			
		}
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.editModel.title="新增产假分配";
		};
		//是否选中
		$scope.viewEmployeeModel.isInAssignmentArr=function(employee){
			if($scope.editModel.maternityLeave.employeeId == employee.employeeId){
				return true;
			}
			return false;
		};
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewEmployeeModel.rowCount,
				rowCount:$scope.viewEmployeeModel.perPage,
				sex:"2",
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		// 点击选择指定人员按钮
		$scope.viewEmployeeModel.employee = function(employee){
			$scope.viewEmployeeModel.show=false;
			$scope.viewModel.show=true;
			$scope.editModel.maternityLeave.employeeNo = employee.employeeNo;
			$scope.editModel.maternityLeave.chineseName = employee.chineseName;
			$scope.editModel.maternityLeave.employeeId = employee.employeeId;
			$scope.editModel.maternityLeave.regionType = employee.regionType;
			$scope.editModel.employeeId = employee.employeeId;
			Service.getHolidayQuota({
				regionType : employee.regionType,
				maternity : "maternity",
			},
			function(data){
				$scope.editModel.maternityLeave.holidayQuota = data;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		}
		// 员工清空
		$scope.editModel.chooseDelete = function(){
			$scope.editModel.maternityLeave.employeeId = null;
		}
} ]);